package com.ronsai.umtrack.base;

import android.app.Activity;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.view.View;

import com.ronsai.umtrack.R;
import com.ronsai.umtrack.util.StatusBarUtil;
import com.trello.rxlifecycle2.components.support.RxAppCompatActivity;
import com.umeng.analytics.MobclickAgent;

import butterknife.ButterKnife;

public abstract class BaseActivity<V extends BaseView, P extends BasePresenter<V>> extends RxAppCompatActivity {

    protected Context mContext;
    private AlertDialog tipsDialog;

    private P presenter;
    private V view;

    public P getPresenter() {
        return presenter;
    }

    protected abstract int getLayoutId();

    protected abstract P createPresenter();

    protected abstract V createView();

    protected abstract void init(Bundle savedInstanceState);

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayoutId());
        ButterKnife.bind(this);
        mContext = this;
        if (presenter == null) {
            presenter = createPresenter();
        }
        if (view == null) {
            view = createView();
        }
        if (presenter != null && view != null) {
            presenter.attachView(view);
        }
        // 设置屏幕方向：修复8.0 Only fullscreen opaque activities can request orientation
        if (Build.VERSION.SDK_INT != Build.VERSION_CODES.O) {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        }
        init(savedInstanceState);
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (presenter != null) {
            presenter.detachView();
        }
    }

    protected void setStatusBarColor() {
        int color = getResources().getColor(R.color.color_50000000);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            color = getResources().getColor(R.color.white);
        }
        StatusBarUtil.setColor(this, color);
        StatusBarUtil.setStatusBarMode(this, true);
    }

    protected void showTipsDialog(String msg) {
        showTipsDialog("提示", msg);
    }

    protected void showTipsDialog(String title, String msg) {
        if (tipsDialog == null) {
            tipsDialog = new AlertDialog.Builder(this)
                    .setTitle(title)
                    .setMessage(msg)
                    .setPositiveButton("确定", null)
                    .create();
        } else if (tipsDialog.isShowing()) {
            tipsDialog.dismiss();
        }
        tipsDialog.setTitle(title);
        tipsDialog.setMessage(msg);
        if (!isFinishing()) {
            tipsDialog.show();
        }
    }
}
